package com.jkt.rms.businessservice.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.jkt.rms.business.Vendor;
import com.jkt.rms.business.VendorHistory;
import com.jkt.rms.businessservice.VendorBusinessService;
import com.jkt.rms.commons.utils.EqualityUtils;
import com.jkt.rms.commons.utils.RMSException;
import com.jkt.rms.constants.IConstants;
import com.jkt.rms.controller.VendorController;
import com.jkt.rms.dataservice.VendorDataService;
import com.jkt.rms.dataservice.VendorHistoryDataService;
import com.jkt.rms.vo.VendorVO;

public class VendorBusinessServiceImpl implements VendorBusinessService {

	private static final Logger logger = Logger.getLogger(VendorController.class);

	
	private VendorDataService vendorDataService;
	private VendorHistoryDataService vendorHistoryDataService;

	public void setVendorHistoryDataService(VendorHistoryDataService vendorHistoryDataService) {
		this.vendorHistoryDataService = vendorHistoryDataService;
	}

	public void setVendorDataService(VendorDataService vendorDataService) {
		this.vendorDataService = vendorDataService;
	}

	public boolean saveVendor(VendorVO vendorVo) throws RMSException{
		boolean isExist = vendorDataService.isVendorExist(vendorVo.getVendorCode(), vendorVo.getVendorName(), null);
		if(!isExist) {
			try {
				vendorVo.setUpdatedBy("User Name");
				vendorVo.setStatus('Y');
				vendorDataService.saveVendor(getVendor(vendorVo));
			} catch (RMSException e) {
				logger.error("Error Occurs in save Vendor :", e.getCause());
				throw new RMSException(e.getBusinessCode(),"Error Occurs while saving the vendor");
			}catch(Exception ex){
				logger.error("Error Occurs in save Vendor :", ex.getCause());
				throw new RMSException("error.application","Error Occurs while saving the vendor");
			}
		} 
		return isExist;
	}

	public boolean updateVendor(VendorVO vendorVO) throws RMSException{
		Long id = Long.valueOf(vendorVO.getId());
		boolean isExist = vendorDataService.isVendorExist(vendorVO.getVendorCode(), vendorVO.getVendorName(),id);
		if(!isExist) {
			try {
				vendorVO.setUpdatedBy("User Name");
				Vendor vendor = vendorDataService.findById(id);
				vendorHistoryDataService.saveVendor(getVendorHistroy(vendor));
				vendorDataService.updateVendor(getVendor(vendorVO));
			} catch (RMSException e) {
				logger.error("Error Occurs in save Vendor :", e.getCause());
				throw new RMSException(e.getBusinessCode(),"Error Occurs while saving the vendor");
			}catch(Exception ex){
				logger.error("Error Occurs in save Vendor :", ex.getCause());
				throw new RMSException("error.application","Error Occurs while saving the vendor");
			}
		} 
		return isExist;
	}

	public List<VendorVO> findAllVendors() {
		final List<Vendor> vendorList = vendorDataService.findAll();
		return  getVendorVoList(vendorList);
	}

	public List<VendorVO> findByCriteria(VendorVO vendorVo) {
		List<Vendor> vendorList = null;
		if((vendorVo.getVendorName() != null && (!vendorVo.getVendorName().equals(IConstants.EMPTY_STRING)))
				|| (vendorVo.getVendorCode() != null && (!vendorVo.getVendorCode().equals(IConstants.EMPTY_STRING)))) {
			try {
				vendorList = vendorDataService.findByCriteria(getVendor(vendorVo));
			} catch (RMSException e) {
				logger.error("Exception occured in search", e.getCause());
			}
		}
		return getVendorVoList(vendorList);
	}

	private VendorHistory getVendorHistroy(Vendor vendor)throws RMSException {
		final VendorHistory vendorHistory = new VendorHistory();
		EqualityUtils.copyProperties(vendorHistory,vendor);
		return vendorHistory;
	}
	
	private Vendor getVendor(VendorVO vendorVo)throws RMSException {
		final Vendor vendor = new Vendor ();
		EqualityUtils.copyProperties(vendor,vendorVo);
		vendor.setUpdatedDate(new Timestamp(System.currentTimeMillis()));
		return vendor;
	}

	private VendorVO getVendorVo(Vendor vendorData) throws RMSException {
		final VendorVO vendorVo = new VendorVO();
		EqualityUtils.copyProperties(vendorVo, vendorData);
		return vendorVo;
	}

	private List<VendorVO> getVendorVoList(List<Vendor> vendorList) {
		List<VendorVO> vendorVoList = null;	
		if(vendorList != null) {
			vendorVoList = new ArrayList<VendorVO>();
			for (Vendor vendor : vendorList) {
				try {
					vendorVoList.add(getVendorVo(vendor));
				} catch (RMSException e) {
					logger.error(" Exception occured in search", e.getCause());
				}
			}
		}
		return vendorVoList;
	}

}
